"ui";
//2.14,2,15
var ColoredButton = require('./uisetting.js');
ui.layout(
    <frame>
        <vertical bg="file://res/熏.jpg" gravity="bottom">
            <input textStyle="bold" text="请输入日期以逗号'，'间隔" id="date" />
            <input textStyle="bold" text="请输起始金额" id="startMoney" />
            <input textStyle="bold" text="请输入每次变化金额并在减少金额前面加上'-'并以逗号'，'隔开" id="changeMoney" />
            <input textStyle="bold" text="请输入年利率，会自动除以365到每天，默认0.3%(0.003)。请写成小数形式" id="rate" />
            <horizontal gravity="center" layout_gravity="bottom">
                <colored-button color="#FFA500" layout_gravity="center" text="计算" id="btn" />
            </horizontal>
        </vertical>
    </frame>
)
ui.statusBarColor("#FFA500")
ui.date.on("long_click", () => {
    ui.date.setText("")
})
ui.startMoney.on("long_click", () => {
    ui.startMoney.setText("")
})
ui.changeMoney.on("long_click", () => {
    ui.changeMoney.setText("")
})
ui.rate.on("long_click", () => {
    ui.rate.setText("")
})
ui.btn.on("click", () => {
    action0 = false
    allDate = ui.date.getText()
    allDateList = String(allDate).replace(/,/g, "，").split("，")
    for (x in allDateList) {
        if (!/^\d+$/.test(allDateList[x])) {
            toastLog("所写日期含有非数字，请检查！" + allDateList[x])
        } else {
            if (allDateList[x].length != 8) {
                toastLog("所写日期格式不正确，请检查！" + allDateList[x])
            } else {
                if (isNaN(Date.parse(allDateList[x].slice(0, 4) + '-' + allDateList[x].slice(4, 6) + '-' + allDateList[x].slice(6, 8)))) {
                    toastLog("所写日期格式不正确，请检查！" + allDateList[x])
                } else {
                    startMoney = ui.startMoney.getText()
                    if (!/-?^[0-9]+.?[0-9]*$/.test(startMoney)) {
                        toastLog("初始金额不正确，请检查！" + startMoney)
                    }
                    else{
                        action0 = true
                    }
                }
            }
        }
    }
    action1 = false
    changeMoney = ui.changeMoney.getText()
    changeMoneyList = String(changeMoney).replace(/,/g, "，").split("，")
    for (x in changeMoneyList) {
        if (isNaN(changeMoneyList[x])) {
                toastLog("所写变化金额格式不正确，请检查！" + changeMoneyList[x])
        }
        else{
            action1 = true
            ratetext = ui.rate.getText()
            if(!/^-?[0-9]+.?[0-9]*$/.test(ratetext)){
                rate = "0.003"
            }
            else{
                rate = ratetext
            }
        }
    }
})